package com.game.framework.orm.jdbc;

import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.SQLFeatureNotSupportedException;
import java.util.logging.Logger;

import javax.sql.DataSource;

/**
 * DataSourceImpl：数据源实现<br>
 * 说明：更换连接池，需要重新实现DataSource<br>
 * @author Liyuan
 */

public class DataSourceImpl implements DataSource {
	private Integer minSize 	= 1;
	private Integer maxSize 	= 3;
	private String	url 		= null;
	private ConnectionPool pool = null;
	
	/** Constructor */
	public DataSourceImpl() {
		super();
	}
	
	public DataSourceImpl(Integer minSize, Integer maxSize, String url) {
		super();
		this.minSize = minSize;
		this.maxSize = maxSize;
		this.url = url;
		init();
	}
	
	/** init */
	private void init(){
		if(url == null || "".equals(url)) return;
		pool = new ConnectionPoolImpl(minSize, maxSize, url);
	}

	/**
	 * 获取连接
	 */
	public Connection getConnection() throws SQLException {
		return pool.getConnection();
	}
	
	/**
	 * 释放连接
	 * @param conn
	 * @throws SQLException
	 */
	public void releaseConnection(Connection conn) throws SQLException{
		if(pool != null && conn != null){
			pool.releaseConnection(conn);
		}
	}
	
	/** 关闭连接池 */
	public void closePool(){
		if(pool != null){
			pool.close();
		}
	}
	
	//其他方法，不用关心
	public Connection getConnection(String username, String password)
			throws SQLException {
		throw new UnsupportedOperationException();
	}

	public int getLoginTimeout() throws SQLException {
		return DriverManager.getLoginTimeout();
	}

	public PrintWriter getLogWriter() throws SQLException {
		return DriverManager.getLogWriter();
	}

	public void setLoginTimeout(int seconds) throws SQLException {
		DriverManager.setLoginTimeout(seconds);
	}

	public boolean isWrapperFor(Class<?> iface) throws SQLException {
		return false;
	}

	public <T> T unwrap(Class<T> iface) throws SQLException {
		return null;
	}

	public void setLogWriter(PrintWriter out) throws SQLException {
	}

	public Logger getParentLogger() throws SQLFeatureNotSupportedException {
		return null;
	}
	
}
